package rad.framework.uml2.uml.edit.providers;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Comparator;
import java.util.Iterator;
import java.util.List;

import org.eclipse.emf.common.notify.AdapterFactory;
import org.eclipse.emf.common.util.EList;
import org.eclipse.emf.common.util.ResourceLocator;
import org.eclipse.emf.ecore.EStructuralFeature;
import org.eclipse.emf.edit.provider.ItemPropertyDescriptor;
import org.eclipse.uml2.common.edit.provider.IItemQualifiedTextProvider;

/**
 * @author boudyacho
 * 
 */
class UMLItemPropertyDescriptor
		extends ItemPropertyDescriptor
		implements IItemQualifiedTextProvider {

	/**
	 * @param adapterFactory
	 * @param resourceLocator
	 * @param displayName
	 * @param description
	 * @param feature
	 * @param isSettable
	 * @param staticImage
	 * @param category
	 * @param filterFlags
	 */
	UMLItemPropertyDescriptor(AdapterFactory adapterFactory, ResourceLocator resourceLocator, String displayName, String description, EStructuralFeature feature, boolean isSettable, Object staticImage, String category, String[] filterFlags) {
		super(adapterFactory, resourceLocator, displayName, description, feature, isSettable, staticImage, category == null && resourceLocator != null ? resourceLocator.getString("_UI_UML_category") : category, filterFlags);

		itemDelegator = new UMLItemDelegator(adapterFactory, resourceLocator);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * org.eclipse.emf.edit.provider.ItemPropertyDescriptor#getChoiceOfValues
	 * (java.lang.Object)
	 */
	@Override
	public Collection getChoiceOfValues(Object object) {
		final Collection comboBoxObjects = getComboBoxObjects(object);

		if (comboBoxObjects != null) {
			final List choiceOfValues = new ArrayList(comboBoxObjects);

			if (choiceOfValues != null) {
				Collections.sort(choiceOfValues, new Comparator() {

					public int compare(Object o1, Object o2) {
						return getQualifiedText(o1).compareTo(getQualifiedText(o2));
					}
				});
			}

			return choiceOfValues;
		}

		return null;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @seeorg.eclipse.uml2.common.edit.provider.IItemQualifiedTextProvider#
	 * getQualifiedText(java.lang.Object)
	 */
	public String getQualifiedText(Object object) {
		return ((IItemQualifiedTextProvider) itemDelegator).getQualifiedText(object);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * org.eclipse.emf.edit.provider.ItemPropertyDescriptor#getId(java.lang.
	 * Object)
	 */
	@Override
	public String getId(Object object) {
		final String category = getCategory(object);
		return category == null ? getQualifiedText(feature) : category + getQualifiedText(feature);
	}

	protected class UMLItemDelegator
			extends ItemDelegator
			implements IItemQualifiedTextProvider {

		protected UMLItemDelegator(AdapterFactory adapterFactory, ResourceLocator resourceLocator) {
			super(adapterFactory, resourceLocator);
		}

		public String getQualifiedText(Object object) {

			if (object instanceof EList) {
				final StringBuffer text = new StringBuffer();

				for (final Iterator i = ((List) object).iterator(); i.hasNext();) {
					final Object child = i.next();

					if (text.length() > 0) {
						text.append(", ");
					}

					text.append(getQualifiedText(child));
				}

				return text.toString();
			} else {
				final IItemQualifiedTextProvider itemQualifiedTextProvider = (IItemQualifiedTextProvider) adapterFactory.adapt(object, IItemQualifiedTextProvider.class);

				return itemQualifiedTextProvider != null ? itemQualifiedTextProvider.getQualifiedText(object) : getText(object);
			}
		}
	}
}